using System;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;

namespace Rest
{
    /// <summary>
    /// A collection of RestDataItem objects
    /// </summary>
    public class RestDataItemCollection : Collection<IRestDataItem>
    {
        /// <summary>
        /// Adds new RestDataItem to the list
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public void Add(String name, Byte[] value)
        {
            Add(name, value, null);
        }

        /// <summary>
        /// Adds new RestDataItem to the list
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="fileName"></param>
        public void Add(String name, Byte[] value, String fileName)
        {
            base.Add(new RestDataItem(name, value, fileName));
        }

        /// <summary>
        /// Adds the supplied FileInfo
        /// </summary>
        /// <param name="name"></param>
        /// <param name="file"></param>
        /// <param name="contentType"></param>
        public void Add(String name, FileInfo file, String contentType)
        {
            base.Add(new RestDataItem(name, file, contentType)); 
        }

        /// <summary>
        /// Adds new RestDataItem to the list
        /// </summary>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="fileName"></param>
        /// <param name="contentType"></param>
        public void Add(String name, Byte[] value, String fileName, 
            String contentType)
        {
            base.Add(new RestDataItem(name, value, contentType));
        }

        /// <summary>
        /// Adds a collection of RestDataItem to the current collection
        /// </summary>
        /// <param name="dataItems"></param>
        public void Add(RestDataItemCollection dataItems)
        {
            foreach (IRestDataItem dataItem in dataItems)
            {
                Add(dataItem);
            }
        }

        /// <summary>
        /// Adds a file to the current collection
        /// </summary>
        /// <param name="fileName"></param>
        /// <param name="contentType"></param>
        /// <exception cref="FileNotFoundException" />
        public void AddFile(String fileName, String contentType) 
        {
            if (!File.Exists(fileName))
            {
                throw new FileNotFoundException("File not found", fileName);
            }

            Add(new RestDataItem(fileName, new FileInfo(fileName), contentType));  
        }
    }
}
